package com.ljf.authority.domain.validator;

import com.ljf.authority.domain.repository.AuthorityBaseMenuTabulationRepository;
import com.ljf.authority.domain.repository.AuthorityDatPermissionGroupRepository;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupCommand;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupMenuCommand;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupUserCommand;
import com.ljf.authority.dto.query.AuthorityBaseMenuTabulationQuery;
import com.ljf.authority.dto.query.AuthorityDatPermissionGroupQuery;
import com.ljf.authority.model.AuthorityDatPermissionGroup;
import com.ljf.authority.vo.AuthorityDatPermissionGroupVO;
import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 权限组  业务校验
 *
 * @author LJF
 * @since 2025-06-08
 */
@Component
@AllArgsConstructor
public class AuthorityDatPermissionGroupValidator extends BasisValidator<AuthorityDatPermissionGroupQuery, AuthorityDatPermissionGroupCommand, AuthorityDatPermissionGroupVO, AuthorityDatPermissionGroup, AuthorityDatPermissionGroupRepository> {

    private final AuthorityBaseMenuTabulationRepository baseMenuTabulationRepository;

    /**
     * 修改权限组菜单 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkEditMenu(AuthorityDatPermissionGroupMenuCommand command) throws ServiceException {
        super.checkIdExist(command.getId());
        if (CollectionUtil.isNotEmpty(command.getMenuList())) {
            int count = baseMenuTabulationRepository.count(AuthorityBaseMenuTabulationQuery.builder().codeList(command.getMenuList()).build());
            if (count != command.getMenuList().size()) {
                throw new ServiceException("校验异常: 菜单不存在, 请刷新后重试");
            }
        }
    }

    /**
     * 修改权限组用户 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkEditUser(AuthorityDatPermissionGroupUserCommand command) throws ServiceException {
        super.checkIdExist(command.getId());
    }

    /**
     * 查询权限组菜单 业务校验
     *
     * @param id
     * @throws ServiceException
     */
    public void checkQueryMenuById(Long id) throws ServiceException {
        super.checkNullId(id);
    }

    /**
     * 查询权限组用户 业务校验
     *
     * @param id
     * @throws ServiceException
     */
    public void checkQueryUserById(Long id) throws ServiceException {
        super.checkNullId(id);
    }

}
